import axios from 'axios'
import QS from 'qs'
import {toStore} from '../stores/counter.js'

import {ElMessage, ElNotification} from "element-plus";
import {router} from "../router/index.js";

// 根据环境切换访问地址
if(process.env.NODE_ENV == 'development'){
    axios.defaults.baseURL='http://192.168.2.49:4100'
}else if(process.env.NODE_ENV=='production'){
    axios.defaults.baseURL="http://192.168.2.49:8000"
}
// 设置超时时间
axios.defaults.timeout=10000;


//拦截请求加token
axios.interceptors.request.use(

    config =>{
        const  store=toStore();
        if(store.token!=""){
            config.headers.token=store.token
        }
        return config;
    },
    error => {
        return Promise.error(error)
    }
)
// 请求结果拦截
axios.interceptors.response.use(

    response=>{
        const  store=toStore();
        if (response.status === 200) {
            // 如果有异常信息则提示出来
            if(response.data.state=="403"){
                ElNotification({
                    title: '超时',
                    message: '请重新登录',
                    type: 'warning',
                })
                store.token=""
                router.push("/")
            }else if (response.data.state!="200"&&response.data.state!=undefined){
                ElMessage.error(response.data.exception)
            }else if(response.headers.get("token")!=undefined){
                store.token=response.headers.get("token")
                localStorage.setItem("store",JSON.stringify(store))
            }
            return Promise.resolve(response);

        } else {
            return Promise.reject(response);
        }
    }
)
// get请求
export function get(url,params){
    return new Promise((resolve,reject)=>{
        axios({
            url:url,
            method:"get",
            params:params
        }).then((res)=>{
            resolve(res.data)
        }).catch((err)=>{
            reject(err)
        })
    })
}
export function post(url,form){
    const formData = new FormData();
    Object.keys(form).forEach((key) => {
        if(key.indexOf("Array")!=-1){
            for (let i = 0; i < form[key].length; i++) {
                formData.append(key.substring(0,key.length-5),form[key][i])
                console.log(key.substring(0,key.length-5))
            }
        }else {
            if(form[key]!=undefined&&form[key]!=undefined&&form[key].length!=0){
                formData.append(key, form[key]);
            }
        }
    });
    return new Promise((resolve,reject)=>{
       axios({
           url:url,
           method:"post",
           data:formData
       }).then((res)=>{
           resolve(res.data)
       }).catch((err)=>{
           reject(err)
       })
    })
}
export function outPost(url,form){
    const formData = new FormData();
    Object.keys(form).forEach((key) => {
        if(key.indexOf("Array")!=-1){
            for (let i = 0; i < form[key].length; i++) {
                formData.append(key.substring(0,key.length-5),form[key][i])
                console.log(key.substring(0,key.length-5))
            }
        }else {
            if(form[key]!=undefined&&form[key].length!=0){
                formData.append(key, form[key]);
            }
        }
    });
    return new Promise((resolve,reject)=>{
        axios({
            url:url,
            method:"post",
            data:formData,
            responseType:'blob'
        }).then((res)=>{
            resolve(res.data)
        }).catch((err)=>{
            reject(err)
        })
    })
}
export function put(url,form){
    const formData = new FormData();
    Object.keys(form).forEach((key) => {
        if(key.indexOf("Array")!=-1){
            for (let i = 0; i < form[key].length; i++) {
                formData.append(key.substring(0,key.length-5),form[key][i])
            }
        }else {
            if(form[key]!=undefined&&form[key].length!=0){
                formData.append(key, form[key]);
            }
        }
    });
    return new Promise((resolve,reject)=>{
        axios({
            url:url,
            method:"put",
            data:formData,
        }).then((res)=>{
            resolve(res.data)
        }).catch((err)=>{
            reject(err)
        })
    })
}
export function remove(url,params){
    return new Promise((resolve,reject)=>{
        axios({
            url:url,
            method:"delete",
            params:params
        }).then((res)=>{
            resolve(res.data)
        }).catch((err)=>{
            reject(err)
        })
    })
}
